angular.module('hyt.personService', ['ngResource'])

  .factory('savePrinter',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.savePrintSet,
      result;

    var resource = $resource(ApiUrl+':prints',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getSavePrint : function(prints){
        console.log(prints);
        return resource.query({
          prints:prints
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('SavePrint.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('getPrinter',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.getPrintSet,
      result;

    var resource = $resource(ApiUrl+':printMsg',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getPrintSet : function(printMsg){
        console.log(printMsg);
        return resource.query({
          printMsg:printMsg
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('GetPrint.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('Version',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.getVersion,
      result;

    var resource = $resource(ApiUrl+':version',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getVersion : function(version){
        return resource.query({
          version:version
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('Version.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('LoadUserDepts',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.loadUserDepts,
      result;

    var resource = $resource(ApiUrl+':loadUserDepts',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getLoadUserDepts : function(loadUserDepts){
        return resource.query({
          loadUserDepts:loadUserDepts
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('LoadUserDepts.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('ChangeDept',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.changeDept,
      result;

    var resource = $resource(ApiUrl+':changeDept',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getChangeDept : function(changeDept){
        return resource.query({
          changeDept:changeDept
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('ChangeDept.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('ChangePwd',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.changePwd,
      result;

    var resource = $resource(ApiUrl+':changePwd',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getChangePwd : function(changePwd){
        return resource.query({
          changePwd:changePwd
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('ChangePwd.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
